//输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。 
//
// 
//
// 示例 1： 
//
// 输入：head = [1,3,2]
//输出：[2,3,1] 
//
// 
//
// 限制： 
//
// 0 <= 链表长度 <= 10000 
// Related Topics 链表 
// 👍 51 👎 0

  
package com.yun.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;

public class CongWeiDaoTouDaYinLianBiaoLcof {
    public static void main(String[] args) {
        Solution solution = new CongWeiDaoTouDaYinLianBiaoLcof().new Solution();
        ListNode node = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        node.next = node2;
        node2.next = node3;
        System.out.println(Arrays.toString(solution.reversePrint(node)));
    }
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    ArrayList<Integer> list = new ArrayList<>();
    public int[] reversePrint(ListNode head) {
        // 1. 先走一遍长度，然后倒数进行填充
        ListNode headTmp = head;
        int len = 0;
        while (headTmp != null) {
            headTmp = headTmp.next;
            len++;
        }
        int[] rs = new int[len];
        int cur = len -1;
        headTmp = head;
        while (headTmp != null) {
            rs[cur--] = headTmp.val;
            headTmp = headTmp.next;
        }
        return rs;

        // 2. 加入堆栈，完了pop出来;
        /*Stack<Integer> stack = new Stack<>();
        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }

        int[] rs = new int[stack.size()];
        int cur = stack.isEmpty()-1;
        while (!stack.isEmpty()) {
            rs[cur--] = stack.pop();
        }
        return rs;*/

        // 3. 递归回溯，加入list
        /*dfs(head);
        System.out.println(list);
        return new int[]{2,2};*/
    }

    private void dfs(ListNode head) {
        if (head == null) {
            return;
        }
        dfs(head.next);
        list.add(head.val);
    }

}
//leetcode submit region end(Prohibit modification and deletion)

}